import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
import pkg from './package.json'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    cssInjectedByJsPlugin()
  ],
  define: {
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
    'process.env': {}, // 防止依赖包访问 process.env
    'process': { env: {} }, // 防止依赖包访问 process
    '__PLUGIN_NAME__': JSON.stringify(pkg.name)
  },
  build: {
    lib: {
      entry: 'src/index.js',
      name: pkg.name,
      fileName: () => `${pkg.name}_${pkg.version}.js`,
      formats: ['iife'], // 或 umd
    },
    rollupOptions: {
      external: ['vue', 'vue-router'],
      output: {
        globals: {
          vue: 'Vue',
          'vue-router': 'VueRouter'
        },
        inlineDynamicImports: true
      }
    }
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:8080/',
        changeOrigin: true,
        rewrite: path => path
      }
    }
  }
})
